package List1;

public class MySingleList implements IList{
    static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public void createList(){
    ListNode node1=new ListNode(12);
    ListNode node2=new ListNode(23);
    ListNode node3=new ListNode(34);
    ListNode node4=new ListNode(45);
    ListNode node5=new ListNode(56);
    node1.next=node2;
    node2.next=node3;
    node3.next=node4;
    node4.next=node5;
    this.head=node1;
}

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public int size(){
        int len=0;
        ListNode cur=head;
        while(cur!=null){
            len++;
            cur=cur.next;
        }
        return len;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
        }
        return false;
    }

    @Override
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
        node.next=head;
        head=node;
    }
    public void addLast(int data){
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    @Override
    public void addIndex(int index, int data) {
        int len=size();
        if(index<0||index>len){
            System.out.println("插入位置不合法");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==len){
            addLast(data);
        }
        //中间插入；
        //首先要找到要插入的中间位置；
        ListNode cur=head;
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        ListNode node=new ListNode(data);
        node.next=cur.next;
        cur.next=node;
    }
    public ListNode findNodeKey(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
        }
        return null;
    }
    public void remove(int key){
        if(head==null){
            head=head.next;
            return;
        }
        ListNode cur=findNodeKey(key);
        if(cur==null){
            return;
        }
        ListNode del=cur.next;
        cur.next=del.next;
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;
    }

    @Override
    public void removeAllKey(int key) {
        ListNode prev=head;
        ListNode cur=head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head==null){
            head=head.next;
        }
    }
    //反转单列表；
    public ListNode reverseList(){
        ListNode cur=head.next;
        if(head==null){
            return null;
        }
        head.next=null;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=head;
            head=cur;
            cur=curN;
        }
        return head;
    }
    //寻找一个单列表的中间节点；
    public ListNode middleNode(){
        if(head==null){
            return head;
        }
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }
//输入一个链表，输出该链表中的倒数第k个节点的值；
    public int kthToLast(int k){
        if(head==null){
            return -1;
        }
        if(k<=0){
            return -1;
        }
        int count=0;
        ListNode fast=head;
        ListNode slow=head;
        while(count!=k-1){
            fast=fast.next;
            if(fast==null){
                return -1;
            }
            count++;
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow.val;
    }
//编写代码，以给定的基准值x，将链表分割成两部分，所有小于x的节点排在大于或等于x的节点之前；
    public ListNode partition(int x){
        ListNode beforeStart=null;
        ListNode beforeEnd=null;
        ListNode afterStart=null;
        ListNode afterEnd=null;
        ListNode cur=head;
        while(cur!=null){
            if(cur.val<x){
                if(beforeStart==null){
                    cur=beforeStart=beforeEnd;
                }else{
                    beforeEnd.next=cur;
                    beforeEnd=beforeEnd.next;
                }
            }else{
                if (afterStart == null) {
                    cur=afterStart=afterEnd;
                }else{
                    afterEnd.next=cur;
                    afterEnd=afterEnd.next;
                }
            }
            cur=cur.next;
        }
        if(beforeStart==null){
            return afterStart;
        }
        beforeEnd=afterStart;
        //不管第二部分最后是否为空都将其设置为空
        if(afterStart==null){
            afterEnd.next=null;
        }
        return beforeStart;
    }
    //判断一个单列表是否是回文结构；
    public boolean chkPalindrome(){
        if(head==null){
            return true;
        }
        //1.寻找中间节点；
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //2.反转链表后半部分；
        ListNode cur=slow.next;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curN;
        }
        //进行从头遍历该链表，判断是否是回文结构；
        while(head!=null){
            if(head.val!= slow.val){
                return false;
            }
            if(head.next==slow){
                return true;
            }
            head=head.next;
            slow=slow.next;
        }
        return true;
    }
    //该定一个链表，判断这个链表是否有环；
    public boolean  haveCycle(){
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }
    //给定一个链表，返回链表开始入环的第一个结点。如果没有环就返回null；
    public ListNode detectCycle(){
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                break;//直接退出该while循环，说明该链表有环；
            }
        }
        if(fast==null||fast.next==null){
            return null;
        }
        slow=head;
        while(fast!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
}